/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package calendarconvert;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import net.fortuna.ical4j.model.Date;
import java.util.StringTokenizer;
import net.fortuna.ical4j.data.CalendarOutputter;
import net.fortuna.ical4j.model.Calendar;
import net.fortuna.ical4j.model.Dur;
import net.fortuna.ical4j.model.Property;
import net.fortuna.ical4j.model.ValidationException;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.model.property.CalScale;
import net.fortuna.ical4j.model.property.Description;
import net.fortuna.ical4j.model.property.ProdId;
import net.fortuna.ical4j.model.property.Version;
import net.fortuna.ical4j.util.UidGenerator;

/**
 *
 * @author Nom
 */
public class CalendarConvertUtil {
    
    public static String[] EVENT_TYPE = {
        "",
        "CALENDAR_EVENTTYPE_RAID",
        "CALENDAR_EVENTTYPE_DUNGEON",
        "CALENDAR_EVENTTYPE_PVP",
        "CALENDAR_EVENTTYPE_MEETING",
        "CALENDAR_EVENTTYPE_OTHER",
    };
    
    public static String[] INVITE_STATUS = {
        "",
        "CALENDAR_INVITESTATUS_INVITED",
        "CALENDAR_INVITESTATUS_ACCEPTED",
        "CALENDAR_INVITESTATUS_DECLINED",
        "CALENDAR_INVITESTATUS_CONFIRMED",
        "CALENDAR_INVITESTATUS_OUT",
        "CALENDAR_INVITESTATUS_STANDBY",
    };
    
    public static void CreateCalendar(CalendarConvertView caller, String outFile) throws FileNotFoundException, IOException, ValidationException {
        caller.addLog("Exporting calendar");
        caller.addLog("Found " + Integer.toString(EventList.getEvents().size()) + " events to save");
        Calendar calendar = new Calendar();
        calendar.getProperties().add(new ProdId("-//WoW Calendar Export//iCal4j 1.0//EN"));
        calendar.getProperties().add(Version.VERSION_2_0);
        calendar.getProperties().add(CalScale.GREGORIAN);
        
        
        UidGenerator ug = new UidGenerator("1");

        for (Event event : EventList.getEvents()) {
            java.util.Calendar cal = java.util.Calendar.getInstance();
            
            System.out.println(event.toString());
            
            StringTokenizer date = new StringTokenizer(event.getDate(), "-");
            StringTokenizer time = new StringTokenizer(event.getTime(), ":");
            
            if (date.countTokens() != 3) {
                caller.addLog("Unknown number of tokens in date (" + date.countTokens() + ")");
            }
            
            if (time.countTokens() != 2) {
                caller.addLog("Unknown number of tokens in time (" + time.countTokens() + ")");
            }
            
            cal.set( 
                    Integer.parseInt(date.nextToken()),   // Year
                    Integer.parseInt(date.nextToken()) - 1,   // Month
                    Integer.parseInt(date.nextToken()),   // Day
                    Integer.parseInt(time.nextToken()),   // Hour
                    Integer.parseInt(time.nextToken())    // Minute
                    );
            cal.clear(java.util.Calendar.SECOND);
            
            VEvent newEvent = new VEvent(
                    new Date(cal.getTime()),
                    new Dur(0, 1, 0, 0),
                    event.getTitle()
            );
            newEvent.getProperties().add(ug.generateUid());
            
            Description description = new Description(
                    "Event Type: " + EVENT_TYPE[event.getEventType()] + "\n" +
                    "Invite Status: " + INVITE_STATUS[event.getInviteStatus()] + "\n" +
                    "Description: " + event.getDescription() + "\n"
                    );
            newEvent.getProperties().add(description);
            
            calendar.getComponents().add(newEvent);
        }
        
        FileOutputStream fout = new FileOutputStream(outFile);
        CalendarOutputter outputter = new CalendarOutputter();
        outputter.output(calendar, fout);
    }
}
